﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MediaLibraryManager;
using System.Configuration;
using System.IO;
using RibbonLib;
using RibbonLib.Interop;
using MS.WindowsAPICodePack.Internal;

namespace MediaLibraryManager.Win
{
	public partial class Main : Form, IRibbonForm
    {
		private MediaLibraryApplicationContext appContext;
		private IMediaEditor CurrentEditor { get; set; }

        public Main()
        {
            InitializeComponent();
        }

		private void Main_Load(object sender, EventArgs e)
		{
			Ribbon.Instance.InitFramework(this);

			appContext = new MediaLibraryApplicationContext(
				SourceApplication.MediaLibraryManager,
				this.DesignMode);

			CurrentEditor = videoLibraryEditor1;
		}

		private void Main_FormClosed(object sender, FormClosedEventArgs e)
		{
			Ribbon.Instance.DestroyFramework();
		}

		private void ProcessAllMediaPaths()
		{
			ProcessMediaPaths(MediaType.MusicVideo);
			ProcessMediaPaths(MediaType.PersonalVideo);
			ProcessMediaPaths(MediaType.PrivateVideo);
			ProcessMediaPaths(MediaType.TVRecording);
			ProcessMediaPaths(MediaType.OtherVideo);
			ProcessMediaPaths(MediaType.Picture);
			ProcessMediaPaths(MediaType.PrivatePicture);
			ProcessMediaPaths(MediaType.Music);
		}

		private void ProcessMediaPaths(MediaType mediaType)
		{
			string paths = appContext.Parameters[String.Format("{0}MonitoredPaths", mediaType.ToString())];
			DirectoryInfo di = null;
			FileInfo[] files = null;

			foreach (string path in paths.Split(';'))
			{
				if (path != String.Empty)
				{
					di = new DirectoryInfo(path);
					files = di.GetFiles();

					foreach (FileInfo file in files)
					{
						appContext.CreateMedia(
							file.Name,
							String.Format(@"{0}\{1}",
								file.DirectoryName,
								file.Name),
							mediaType);

						System.Threading.Thread.Sleep(3000);
					}
				}
			}
		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			ProcessAllMediaPaths();
		}

		#region IRibbonForm Members

		public IntPtr WindowHandle
		{
			get
			{
				return this.Handle;
			}
		}

		public void RibbonHeightUpdated(int newHeight)
		{
			this.splitContainer.SplitterDistance = newHeight;
		}

		#endregion

		#region IUICommandHandler Members

		public HRESULT Execute(uint commandId, RibbonLib.Interop.UI_ExecutionVerb verb, ref Microsoft.WindowsAPICodePack.Shell.PropertySystem.PropertyKey key, ref PropVariant currentValue, RibbonLib.Interop.IUISimplePropertySet commandExecutionProperties)
		{
			if ((commandId == (uint)RibbonMarkupCommands.SetAutoCommit) &&
				(verb == UI_ExecutionVerb.Execute))
			{
				CurrentEditor.AutoCommit = (bool)currentValue.Value;
			}

			return HRESULT.S_OK;
		}

		public HRESULT UpdateProperty(uint commandId, ref Microsoft.WindowsAPICodePack.Shell.PropertySystem.PropertyKey key, ref PropVariant currentValue, ref PropVariant newValue)
		{
			return HRESULT.S_OK;
		}

		#endregion
	}
}
